package com.advertisementsystem.Servie.Impl;

import com.advertisementsystem.Dto.LoginUser;
import com.advertisementsystem.Exception.BusinessException;
import com.advertisementsystem.Mapper.AdvertisementMapper;
import com.advertisementsystem.Mapper.DeviceMapper;
import com.advertisementsystem.Mapper.StatisticsMapper;
import com.advertisementsystem.Servie.StatisticsService;
import com.advertisementsystem.entity.Advertisement;
import com.advertisementsystem.entity.Device;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class StatisticsServiceImpl implements StatisticsService {

    @Autowired
    private StatisticsMapper statisticsMapper;

    @Autowired
    private AdvertisementMapper advertisementMapper;

    @Autowired
    private DeviceMapper deviceMapper;

    @Override
    public Map<String, Object> getAdvertisementStatistics(Long adId) {
        // 检查广告是否存在
        Advertisement ad = advertisementMapper.selectById(adId);
        if (ad == null) {
            throw new BusinessException("广告不存在");
        }

        // 检查权限
        LoginUser loginUser = (LoginUser) SecurityContextHolder.getContext()
                .getAuthentication()
                .getPrincipal();
        if (!loginUser.getPermissions().contains("ROLE_ADMIN") &&
                !ad.getAdvertiserId().equals(loginUser.getUser().getId())) {
            throw new BusinessException("无权查看此广告统计");
        }

        // 获取统计数据
        Map<String, Object> statistics = new HashMap<>();

        // 总播放次数
        Integer totalPlayCount = statisticsMapper.getTotalPlayCount(adId);
        statistics.put("totalPlayCount", totalPlayCount);

        // 总观看次数
        Integer totalViewCount = statisticsMapper.getTotalViewCount(adId);
        statistics.put("totalViewCount", totalViewCount);

        // 日期维度统计
        List<Map<String, Object>> dailyStats = statisticsMapper.getDailyStatistics(adId);
        statistics.put("dailyStats", dailyStats);

        // 设备维度统计
        List<Map<String, Object>> deviceStats = statisticsMapper.getDeviceStatistics(adId);
        statistics.put("deviceStats", deviceStats);

        return statistics;
    }

    @Override
    public Map<String, Object> getDeviceStatistics(Long deviceId) {
        // 检查设备是否存在
        Device device = deviceMapper.selectById(deviceId);
        if (device == null) {
            throw new BusinessException("设备不存在");
        }

        // 检查权限
        LoginUser loginUser = (LoginUser) SecurityContextHolder.getContext()
                .getAuthentication()
                .getPrincipal();
        if (!loginUser.getPermissions().contains("ROLE_ADMIN") &&
                !device.getOwnerId().equals(loginUser.getUser().getId())) {
            throw new BusinessException("无权查看此设备统计");
        }

        // 获取统计数据
        Map<String, Object> statistics = new HashMap<>();

        // 总播放次数
        Integer totalPlayCount = statisticsMapper.getDeviceTotalPlayCount(deviceId);
        statistics.put("totalPlayCount", totalPlayCount);

        // 总观看次数
        Integer totalViewCount = statisticsMapper.getDeviceTotalViewCount(deviceId);
        statistics.put("totalViewCount", totalViewCount);

        // 日期维度统计
        List<Map<String, Object>> dailyStats = statisticsMapper.getDeviceDailyStatistics(deviceId);
        statistics.put("dailyStats", dailyStats);

        // 广告维度统计
        List<Map<String, Object>> adStats = statisticsMapper.getDeviceAdStatistics(deviceId);
        statistics.put("adStats", adStats);

        return statistics;
    }

    @Override
    public Map<String, Object> getOverallStatistics(LocalDate startDate, LocalDate endDate) {
        // 检查权限
        LoginUser loginUser = (LoginUser) SecurityContextHolder.getContext()
                .getAuthentication()
                .getPrincipal();
        if (!loginUser.getPermissions().contains("ROLE_ADMIN")) {
            throw new BusinessException("无权查看整体统计");
        }

        Map<String, Object> statistics = new HashMap<>();

        // 广告总数
        Integer totalAds = statisticsMapper.getTotalAds(startDate, endDate);
        statistics.put("totalAds", totalAds);

        // 设备总数
        Integer totalDevices = statisticsMapper.getTotalDevices(startDate, endDate);
        statistics.put("totalDevices", totalDevices);

        // 总播放次数
        Integer totalPlayCount = statisticsMapper.getOverallPlayCount(startDate, endDate);
        statistics.put("totalPlayCount", totalPlayCount);

        // 总观看次数
        Integer totalViewCount = statisticsMapper.getOverallViewCount(startDate, endDate);
        statistics.put("totalViewCount", totalViewCount);

        // 日期维度统计
        List<Map<String, Object>> dailyStats = statisticsMapper.getOverallDailyStatistics(startDate, endDate);
        statistics.put("dailyStats", dailyStats);

        return statistics;
    }
}
